<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: pn_sofortueberweisung.php 273 2013-09-23 08:48:08Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 273 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-23 08:48:08 +0000 (Mon, 23 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require_once(DIR_FS_CATALOG.'callback/pn_sofortueberweisung/classPnSofortueberweisung.php');
require_once(DIR_FS_INC.'inc.build_order_id.php');

class pn_sofortueberweisung {

	var $code, $title, $description, $enabled, $pnSofortueberweisung, $db;
	function __construct() {
		global $order, $db;
		$this->code = 'pn_sofortueberweisung';
		$this->version = 'SEO:mercari';
		$this->title = MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_TEXT_TITLE;
		if(MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_KS_STATUS == 'True') {
			$this->title = MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_KS_TEXT_TITLE;
			$this->version .= 'k';
		}
			
		$this->description = MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_TEXT_DESCRIPTION;
		$this->sort_order = MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_SORT_ORDER;
		$this->enabled = ((MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_STATUS == 'True') ? true : false);
		$this->info = MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_TEXT_INFO;
		$this->tmpOrders = true;
		$this->tmpStatus = MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_TMP_STATUS_ID;
		if ((int) MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_ORDER_STATUS_ID > 0)
			$this->order_status = MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_ORDER_STATUS_ID;

		if (is_object($order))
			$this->update_status();

		$this->defaultCurrency = DEFAULT_CURRENCY;
		$this->pnSofortueberweisung = new classPnSofortueberweisung(MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_PROJECT_PASSWORD, MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_HASH_ALGORITHM);
		$this->form_action_url = $this->pnSofortueberweisung->formActionUrl;
		$this->pnSofortueberweisung->version = $this->version; 
		$this->db = $db;
		$this->returnURL = (CHECKOUT_AJAX_STAT == 'true' ? FILENAME_CHECKOUT : FILENAME_CHECKOUT_PAYMENT);
	}
	
	function update_status () {
		global $order;
		if (($this->enabled == true) && ((int) MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_ZONE > 0)) {
			$check_flag = false;
			$check = $this->db->db_query("SELECT zone_id FROM ".TABLE_ZONES_TO_GEO_ZONES." WHERE geo_zone_id = '".MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_ZONE."' and zone_country_id = '".$order->billing['country']['id']."' ORDER BY zone_id");
			while (!$check->EOF) {
				if ($check->fields['zone_id'] < 1) {
					$check_flag = true;
					break;
				} elseif ($check->fields['zone_id'] == $order->billing['zone_id']) {
					$check_flag = true;
					break;
				}
				$check->MoveNext();
			}
			if ($check_flag == false) {
				$this->enabled = false;
			}
		}
	}
	
	function javascript_validation () {
		return false;
	}
	
	function selection () {
		if (!empty($_SESSION['cart_pn_sofortueberweisung_ID'])) {
			$order_id = substr($_SESSION['cart_pn_sofortueberweisung_ID'], strpos($_SESSION['cart_pn_sofortueberweisung_ID'], '-') + 1);
			$result = $this->db->db_query("SELECT orders_status FROM ".TABLE_ORDERS." WHERE orders_id = '".(int)$order_id."' LIMIT 1");
			if($result->_numOfRows) {
				if ($result->fields['orders_status'] == MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_TMP_STATUS_ID) {
					$this->_remove_order( (int) $order_id, 'on');
					unset($_SESSION['cart_pn_sofortueberweisung_ID']);
					unset($_SESSION['tmp_oID']);
				}
			}
		}
		$title = '';
		switch (MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_IMAGE) {
			case 'Logo & Text':
				$image = image(sprintf('lang/%s/modules/payment/images/sofortueberweisung_logo.gif', $_SESSION['language']), MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_TEXT_DESCRIPTION_CHECKOUT_PAYMENT_IMAGEALT);
				if(MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_KS_STATUS == 'True')
					$title = str_replace('{{image}}', $image, sprintf(MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_TEXT_DESCRIPTION_CHECKOUT_PAYMENT_IMAGE, MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_KS_TEXT_DESCRIPTION_CHECKOUT_PAYMENT_TEXT));
				else
					$title = str_replace('{{image}}', $image, sprintf(MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_TEXT_DESCRIPTION_CHECKOUT_PAYMENT_IMAGE, MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_TEXT_DESCRIPTION_CHECKOUT_PAYMENT_TEXT));
				break;
			case 'Logo':
				$image = image(sprintf('lang/%s/modules/payment/images/sofortueberweisung_logo.gif', $_SESSION['language']), MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_TEXT_DESCRIPTION_CHECKOUT_PAYMENT_IMAGEALT);
				$title = str_replace('{{image}}', $image, sprintf(MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_TEXT_DESCRIPTION_CHECKOUT_PAYMENT_IMAGE, ''));
				break;
			case 'Infographic':
				if(MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_KS_STATUS == 'True')
					$image = image(sprintf('lang/%s/modules/payment/images/sofortueberweisungk_banner.jpg', $_SESSION['language']), MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_TEXT_DESCRIPTION_CHECKOUT_PAYMENT_IMAGEALT);
				else
					$image = image(sprintf('lang/%s/modules/payment/images/sofortueberweisung_info.gif', $_SESSION['language']), MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_TEXT_DESCRIPTION_CHECKOUT_PAYMENT_IMAGEALT);
				$title = str_replace('{{image}}', $image, sprintf(MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_TEXT_DESCRIPTION_CHECKOUT_PAYMENT_IMAGE, ''));
				break;
		}
		return array('id' => $this->code , 'module' => $this->title , 'description' => $title);
	}
	
	function pre_confirmation_check () {
		// Fix for XTC Bug
		// We need a cartID
		if(empty($_SESSION['cart']->cartID))
			$_SESSION['cart']->cartID = $_SESSION['cart']->generate_cart_id();
		return false;
	}
	
	function confirmation () {
		global $order;

		/* If temporary order is still in session, check if order ID exists and delete order and all relating (session) data
		 * User might have returned to the shop for changing the order or payment method
		 */
		if (!empty($_SESSION['cart_pn_sofortueberweisung_ID'])) {
			$order_id = substr($_SESSION['cart_pn_sofortueberweisung_ID'], strpos($_SESSION['cart_pn_sofortueberweisung_ID'], '-') + 1);
			$cartID = substr($_SESSION['cart_pn_sofortueberweisung_ID'], 0, strlen($_SESSION['cart']->cartID));
			$result = $this->db->db_query("SELECT currency, orders_status FROM ".TABLE_ORDERS." WHERE orders_id = '".(int) $order_id."'");
			if (($result->fields['orders_status'] == MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_TMP_STATUS_ID) || ($result->fields['currency'] != $order->info['currency']) || ($_SESSION['cart']->cartID != $cartID)) {
				$this->_remove_order( (int) $order_id, 'on');
				unset($_SESSION['cart_pn_sofortueberweisung_ID']);
				unset($_SESSION['tmp_oID']);
			}
		}
		return false;
	}
	
	function process_button () {
		return false;
	}
	
	function before_process () {
		return false;
	}
	
	function payment_action () {
		global $order, $price, $insert_id;

		$customer_id = $_SESSION['customer_id'];
		$order_id = $insert_id;
		$_SESSION['cart_pn_sofortueberweisung_ID'] = $_SESSION['cart']->cartID.'-'.$insert_id;

    $ot_total = $price->_ext->get_total_values();
		if ($_SESSION['customers_status']['customers_status_show_price_tax'] == 0 && $_SESSION['customers_status']['customers_status_add_tax_ot'] == 1)
			$total = $ot_total['ot_total'] + $ot_total['ot_tax'];
		else
			$total = $ot_total['ot_total'];

		$amount = round($total, $price->get_decimal_places($_SESSION['currency']));
		$amount = number_format($amount, 2, '.', '');
		
		$_SESSION['sofortueberweisung_total'] = $amount;
		$parameter = array();
		$currency = $_SESSION['currency'];

		$reason_1 = str_replace('{{order_id}}', build_order_id(strftime(DATE_FORMAT_SHORT), $order_id, 'confirm'), MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_REASON_1);
		$reason_1 = str_replace('{{customer_id}}', $customer_id, $reason_1);
		$reason_1 = substr($reason_1, 0, 27);
		
		$reason_2 = str_replace('{{order_id}}', $order_id, MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_TEXT_REASON_2);
		$reason_2 = str_replace('{{customer_id}}', $customer_id, $reason_2);
		$reason_2 = str_replace('{{order_date}}', strftime(DATE_FORMAT_SHORT), $reason_2);
		$reason_2 = str_replace('{{customer_name}}', $order->customer['firstname'].' '.$order->customer['lastname'], $reason_2);
		$reason_2 = str_replace('{{customer_company}}', $order->customer['company'], $reason_2);
		$reason_2 = str_replace('{{customer_email}}', $order->customer['email_address'], $reason_2);
		$reason_2 = substr($reason_2, 0, 27);

		$user_variable_0 = $order_id;
		$user_variable_1 = $customer_id;

		if (ENABLE_SSL == true)
			$server = HTTPS_SERVER;
		else
			$server = HTTP_SERVER;
			
		$server = str_replace('https://', '', $server);
		$server = str_replace('http://', '', $server);
		

		// success return url:
		$user_variable_2 = $server.DIR_WS_CATALOG.FILENAME_CHECKOUT_PROCESS;

		// cancel return url:
		$user_variable_3 = $server.DIR_WS_CATALOG.$this->returnURL.'?payment_error=pn_sofortueberweisung';

		// notification url: 
		$user_variable_4 = '';

		$user_variable_5 = $_SESSION['cart']->cartID;
		

		// Additionally update status
		$sql_data_array = array('orders_id' => (int)$order_id , 
								'orders_status_id' => MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_TMP_STATUS_ID , 
								'date_added' => 'now()', 
								'customer_notified' => '0', 
								'comments' => MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_TEXT_TITLE);
		$this->db->db_perform(TABLE_ORDERS_STATUS_HISTORY, $sql_data_array);

		$url = $this->pnSofortueberweisung->getPaymentUrl(MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_USER_ID, 
			MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_PROJECT_ID, $amount, $currency, $reason_1, $reason_2, 
			$user_variable_0, $user_variable_1, $user_variable_2, $user_variable_3, $user_variable_4, $user_variable_5);

		redirect($url);
	}
	function after_process () {
		/* Clear our session data
		 * All other session data will be handled in checkout_process.php
		 */
		if (isset($_SESSION)) {
			if (isset($_SESSION['cart_pn_sofortueberweisung_ID'])) 
				unset($_SESSION['cart_pn_sofortueberweisung_ID']);
			if (isset($_SESSION['sofortueberweisung_total'])) 
				unset($_SESSION['sofortueberweisung_total']);
		}
	}
	function output_error (){
		return false;
	}
	function check () {
		if (!isset($this->_check)) {
			$check = $this->db->db_query("SELECT configuration_value FROM ".TABLE_CONFIGURATION." WHERE configuration_key = 'MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_STATUS'");
			$this->_check = $check->_numOfRows;

			//if old installation and notification password not set we need to upgrade the database
			if (defined('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_STATUS')	&& (MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_STATUS == 'True')) {
				if(!defined('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_PROJECT_NOTIF_PASSWORD')) {
					$check = $this->db->db_query("SELECT configuration_value FROM ".TABLE_CONFIGURATION." WHERE configuration_key = 'MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_PROJECT_NOTIF_PASSWORD'");
					if($check->_numOfRows < 1) {
						$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." ( configuration_key, configuration_value,  configuration_group_id, sort_order, date_added) VALUES ('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_PROJECT_NOTIF_PASSWORD', '".MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_PROJECT_PASSWORD."',  '6', '4', now())");
						$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." ( configuration_key, configuration_value,  configuration_group_id, sort_order, date_added) VALUES ('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_HASH_ALGORITHM', 'sha1',  '6', '4', now())");
					}
				}
				if(!defined('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_RECEIVED_STATUS_ID')) {
					$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." ( configuration_key, configuration_value,  configuration_group_id, sort_order, set_function, use_function, date_added) VALUES ('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_RECEIVED_STATUS_ID', '0',  '6', '11', 'cfg_pull_down_order_statuses(', 'get_order_status_name', now())");
					$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." ( configuration_key, configuration_value,  configuration_group_id, sort_order, set_function, use_function, date_added) VALUES ('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_LOSS_STATUS_ID', '0',  '6', '12', 'cfg_pull_down_order_statuses(', 'get_order_status_name', now())");
					$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." ( configuration_key, configuration_value,  configuration_group_id, sort_order, set_function, date_added) VALUES ('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_KS_STATUS', 'False', '6', '3', 'cfg_select_option(array(\'True\', \'False\'), ', now())");
				}
			}
		}
		return $this->_check;
	}
	function get_error () {
		$error = false;
		if (! empty($_GET['payment_error'])) {
			$error = array('title' => MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_TEXT_ERROR_HEADING , 'error' => MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_TEXT_ERROR_MESSAGE);
		}
		return $error;
	}

	function autoinstall() {
		$backlink = href_link('modules_payment.php', 'set=payment&module=pn_sofortueberweisung&action=install', 'SSL');
		
		$header_redir_url = 'http://-USER_VARIABLE_2-';
		if (ENABLE_SSL_CATALOG == 'true' && strpos(HTTPS_CATALOG_SERVER, 'tps://') === 2)
			$header_redir_url = 'https://-USER_VARIABLE_2-';

		$html_abortlink = 'http://-USER_VARIABLE_3-';
		if (ENABLE_SSL_CATALOG == 'true' && strpos(HTTPS_CATALOG_SERVER, 'tps://') === 2)
			$html_abortlink = 'https://-USER_VARIABLE_3-';
			
		$alert_http_url = HTTP_SERVER.DIR_WS_CATALOG.'callback/pn_sofortueberweisung/callback.php';
		if (ENABLE_SSL_CATALOG == 'true' && strpos(HTTPS_CATALOG_SERVER, 'tps://') === 2)
			$alert_http_url = HTTPS_SERVER.DIR_WS_CATALOG.'callback/pn_sofortueberweisung/callback.php';


		$html = $this->pnSofortueberweisung->getAutoInstallPage(STORE_NAME, catalog_href_link(), STORE_OWNER_EMAIL_ADDRESS, DEFAULT_LANGUAGE,
			DEFAULT_CURRENCY, $html_abortlink, $header_redir_url, $alert_http_url, $backlink, 304);
			
		$_SESSION['pn_sofortueberweisung_pw'] = $this->pnSofortueberweisung->password;
		$_SESSION['pn_sofortueberweisung_pw2'] = $this->pnSofortueberweisung->password2;
		$_SESSION['pn_sofortueberweisung_hashAlgorithm'] = $this->pnSofortueberweisung->hashfunction;
		
		return $html;
	}
	
	function install () {

		if (isset($_GET['autoinstall']) && ($_GET['autoinstall'] == '1')) {
			// Module already installed
			if (defined('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_STATUS') && (MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_STATUS == 'True')) {
				redirect(href_link_admin('admin/modules_payment.php', 'set=payment&module=pn_sofortueberweisung', 'SSL'));
			}
			print $this->autoinstall();
			exit();
		} else {

			$user_id = (isset($_GET['user_id'])) ? $_GET['user_id'] : '10000';
			$project_id = (isset($_GET['project_id'])) ? $_GET['project_id'] : '500000';
			if(isset($_GET['consumer_protection']) && $_GET['consumer_protection'] == '1')
				$consumer_protection = 'True';
			else
				$consumer_protection = 'False';
			
			if (isset($_SESSION['pn_sofortueberweisung_pw']) && !empty($_SESSION['pn_sofortueberweisung_pw'])) {
				$project_password = $_SESSION['pn_sofortueberweisung_pw'];
				unset($_SESSION['pn_sofortueberweisung_pw']);
			} else $project_password = '';
			if (isset($_SESSION['pn_sofortueberweisung_pw2']) && !empty($_SESSION['pn_sofortueberweisung_pw2'])) {
				$project_password2 = $_SESSION['pn_sofortueberweisung_pw2'];
				unset($_SESSION['pn_sofortueberweisung_pw2']);
			} else $project_password2 = '';
			if (isset($_SESSION['pn_sofortueberweisung_hashAlgorithm']) && !empty($_SESSION['pn_sofortueberweisung_hashAlgorithm'])) {
				$hashAlgorithm = $_SESSION['pn_sofortueberweisung_hashAlgorithm'];
				unset($_SESSION['pn_sofortueberweisung_hashAlgorithm']);
			} else $hashAlgorithm = $this->pnSofortueberweisung->getSupportedHashAlgorithm();

			
			$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." ( configuration_key, configuration_value,  configuration_group_id, sort_order, set_function, date_added) VALUES ('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_STATUS', 'True', '6', '3', 'cfg_select_option(array(\'True\', \'False\'), ', now())");
			$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." ( configuration_key, configuration_value,  configuration_group_id, sort_order, set_function, date_added) VALUES ('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_KS_STATUS', '".$consumer_protection."', '6', '3', 'cfg_select_option(array(\'True\', \'False\'), ', now())");
			$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." ( configuration_key, configuration_value,  configuration_group_id, sort_order, date_added) VALUES ('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_ALLOWED', '', '6', '0', now())");
			$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." ( configuration_key, configuration_value,  configuration_group_id, sort_order, date_added) VALUES ('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_USER_ID', '".(int) $user_id."',  '6', '4', now())");
			$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." ( configuration_key, configuration_value,  configuration_group_id, sort_order, date_added) VALUES ('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_PROJECT_ID', '".(int) $project_id."',  '6', '4', now())");
			$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." ( configuration_key, configuration_value,  configuration_group_id, sort_order, date_added) VALUES ('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_PROJECT_PASSWORD', '". $project_password ."',  '6', '4', now())");
			$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." ( configuration_key, configuration_value,  configuration_group_id, sort_order, date_added) VALUES ('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_PROJECT_NOTIF_PASSWORD', '". $project_password2 ."',  '6', '4', now())");
			$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." ( configuration_key, configuration_value,  configuration_group_id, sort_order, date_added) VALUES ('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_HASH_ALGORITHM', '". $hashAlgorithm ."',  '6', '4', now())");
			$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." ( configuration_key, configuration_value,  configuration_group_id, sort_order, date_added) VALUES ('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_SORT_ORDER', '1', '6', '20', now())");
			$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." ( configuration_key, configuration_value,  configuration_group_id, sort_order, use_function, set_function, date_added) VALUES ('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_ZONE', '0', '6', '2', 'get_zone_class_title', 'cfg_pull_down_zone_classes(', now())");
			$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." ( configuration_key, configuration_value,  configuration_group_id, sort_order, set_function, use_function, date_added) VALUES ('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_ORDER_STATUS_ID', '0',  '6', '10', 'cfg_pull_down_order_statuses(', 'get_order_status_name', now())");
			$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." ( configuration_key, configuration_value,  configuration_group_id, sort_order, set_function, use_function, date_added) VALUES ('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_TMP_STATUS_ID', '0',  '6', '8', 'cfg_pull_down_order_statuses(', 'get_order_status_name', now())");
			$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." ( configuration_key, configuration_value,  configuration_group_id, sort_order, set_function, use_function, date_added) VALUES ('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_UNC_STATUS_ID', '0',  '6', '9', 'cfg_pull_down_order_statuses(', 'get_order_status_name', now())");
			$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." ( configuration_key, configuration_value,  configuration_group_id, sort_order, set_function, use_function, date_added) VALUES ('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_RECEIVED_STATUS_ID', '0',  '6', '11', 'cfg_pull_down_order_statuses(', 'get_order_status_name', now())");
			$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." ( configuration_key, configuration_value,  configuration_group_id, sort_order, set_function, use_function, date_added) VALUES ('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_LOSS_STATUS_ID', '0',  '6', '12', 'cfg_pull_down_order_statuses(', 'get_order_status_name', now())");
			$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." ( configuration_key, configuration_value,  configuration_group_id, sort_order, set_function, date_added) VALUES ('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_REASON_1', 'Nr. {{order_id}} Kd-Nr. {{customer_id}}',  '6', '4', 'cfg_select_option(array(\'Nr. {{order_id}} Kd-Nr. {{customer_id}}\',\'-TRANSACTION-\'), ', now())");
			$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." ( configuration_key, configuration_value,  configuration_group_id, sort_order, date_added) VALUES ('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_TEXT_REASON_2', '".addslashes(STORE_NAME)."', '6', '4', now())");
			$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." ( configuration_key, configuration_value,  configuration_group_id, sort_order, set_function, date_added) VALUES ('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_IMAGE', 'Infographic',  '6', '6', 'cfg_select_option(array(\'Infographic\',\'Logo & Text\',\'Logo\'), ', now())");
		}
	}
	
	function remove () {
		$this->db->db_query("DELETE FROM ".TABLE_CONFIGURATION." WHERE configuration_key IN ('".implode("', '", $this->keys())."')");
		$this->db->db_query("DELETE FROM ".TABLE_CONFIGURATION." WHERE configuration_key = 'MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_HASH_ALGORITHM'");
	}
	
	function keys () {
		return array('MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_STATUS' ,
		'MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_KS_STATUS', 
		'MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_ALLOWED' , 
		'MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_USER_ID' , 
		'MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_PROJECT_ID' , 
		'MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_PROJECT_PASSWORD',  
		'MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_PROJECT_NOTIF_PASSWORD', 
		'MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_ZONE' , 
		'MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_REASON_1', 
		'MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_TEXT_REASON_2' , 
		'MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_IMAGE' , 
		'MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_TMP_STATUS_ID' , 
		'MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_UNC_STATUS_ID' , 
		'MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_ORDER_STATUS_ID' , 
		'MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_RECEIVED_STATUS_ID',
		'MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_LOSS_STATUS_ID',
		'MODULE_PAYMENT_PN_SOFORTUEBERWEISUNG_SORT_ORDER');
	}

	function _remove_order($order_id, $restock = false, $canceled = false) {
		if ($restock == 'on') {
			$order = $this->db->db_query("SELECT orders_products_id, products_id, products_quantity from ".TABLE_ORDERS_PRODUCTS." WHERE orders_id = ".$this->db->db_prepare($order_id));
			while (!$order->EOF) {
				$this->db->db_query("UPDATE ".TABLE_PRODUCTS." set products_quantity = products_quantity + ".$order->fields['products_quantity'].", products_ordered = products_ordered - ".$order->fields['products_quantity']." WHERE products_id = '".$order->fields['products_id']."'");
				if(ATTRIBUTE_STOCK_CHECK == 'true'){
					$orders_attributes = $this->db->db_query("SELECT
																	products_options, 
																	products_options_values 
																FROM 
																	".TABLE_ORDERS_PRODUCTS_ATTRIBUTES." 
																WHERE 
																	orders_id = ".$this->db->db_prepare($order_id)."
																AND 
																	orders_products_id = '".$order->fields['orders_products_id']."'");
					while(!$orders_attributes->EOF) {
						$attributes_id = $this->db->db_query("SELECT
																	pa.products_attributes_id	
																FROM 
																	".TABLE_PRODUCTS_OPTIONS_VALUES." pov, 
																	".TABLE_PRODUCTS_OPTIONS." po, 
																	".TABLE_PRODUCTS_ATTRIBUTES." pa 
																WHERE 
																	po.products_options_name = '".$orders_attributes->fields['products_options']."'
																AND 
																	po.products_options_id = pa.options_id
																AND 
																	pov.products_options_values_id = pa.options_values_id
																AND 
																	pov.products_options_values_name = '".$orders_attributes->fields['products_options_values']."'
																AND 
																	pa.products_id = '".$order->fields['products_id']."'
																LIMIT 1");
						if($attributes_id->numOfRows == 1){
							$this->db->db_query("UPDATE 
													".TABLE_PRODUCTS_ATTRIBUTES."
												SET 
													attributes_stock = attributes_stock + ".$order->fields['products_quantity']." 
												WHERE
													products_attributes_id = '".$attributes_id->fields['products_attributes_id']."'");
						}
						$orders_attributes->MoveNext();
					}
				}
				$order->MoveNext();
			}
		}

		if(!$canceled) {
			$this->db->db_query("DELETE FROM ".TABLE_ORDERS." WHERE orders_id = ".$this->db->db_prepare($order_id));
			$this->db->db_query("DELETE FROM ".TABLE_ORDERS_PRODUCTS." WHERE orders_id = ".$this->db->db_prepare($order_id));
			$this->db->db_query("DELETE FROM ".TABLE_ORDERS_PRODUCTS_ATTRIBUTES." WHERE orders_id = ".$this->db->db_prepare($order_id));
			$this->db->db_query("DELETE FROM ".TABLE_ORDERS_STATUS_HISTORY." WHERE orders_id = ".$this->db->db_prepare($order_id));
			$this->db->db_query("DELETE FROM ".TABLE_ORDERS_TOTAL." WHERE orders_id = ".$this->db->db_prepare($order_id));
		}
	}
}
?>